<?php
/**
 * 文件名：tie.mod.php
 * 版本号：1.0
 * 作者：mrasong
 * 网站：http://mrasong.com
 * 功能描述：招贴模块
 */
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject{
	var $ID = 0;

	function ModuleObject($config)
	{
		$this->MasterObject($config);
		if(isset($this->Get['id'])){
			$this->ID = (int)$this->Get['id'];
		}elseif(isset($this->Post['id'])){
			$this->ID = (int)$this->Post['id'];
		}
		
		Load::logic('topic');
		$this->TopicLogic = new TopicLogic();
		
				$this->my = array();
		if (MEMBER_ID < 1) {
			$this->Messager("请先登录");	
		}
		
		$this->my = $this->TopicLogic->GetMember(MEMBER_ID);
		$this->tie_category = array(0=>"分享物品",1=>"团购信息",2=>"拼车",3=>"物品求购");
		$this->Execute();
	}

	function Execute(){
		ob_start();
		
		switch($this->Code){

			case "my":
				$this->my();
				break;
			case "myfav":
				$this->myfav();
				break;
			case "add":
				$this->add();
				break;
			case "addsave":
				$this->addsave();
				break;
			case "del":
				$this->del();
				break;
			default:
				$this->Main();
				break;
		}
		$Contents=ob_get_clean();
		$this->ShowBody($Contents);
	}

	function Main(){
		$tie_category = $this->tie_category;

		$ties = array(); 
		foreach( $tie_category as $key=>$val ){
			$sql = "select a.*,b.username,b.nickname,b.face from `".TABLE_PREFIX."tie` as a
						left join `".TABLE_PREFIX."members` as b on a.uid = b.uid
						where a.cate='$key' order by a.addtime desc limit 20";
			$query = $this->DatabaseHandler->Query($sql);
		
			while($row = $query->GetRow()){
				$ties[$val][] = $row;
			}
		}

		include(ROOT_PATH . './templates/app/tie_index.php');
	}
	
	
	function add(){
		$tie_category = $this->tie_category;
		include(ROOT_PATH . './templates/app/tie_add.php');
	}
	

	function my(){
		$tie_category = $this->tie_category;

		$ties = array(); 

		$sql = "select a.*,b.username,b.nickname,b.face from `".TABLE_PREFIX."tie` as a
					left join `".TABLE_PREFIX."members` as b on a.uid = b.uid
					where a.uid = {$_COOKIE['MEMBER_ID']}  order by a.addtime desc";
		$query = $this->DatabaseHandler->Query($sql);
	
		while($row = $query->GetRow()){
			$ties[] = $row;
		}

		include(ROOT_PATH . './templates/app/tie_my.php');
	}

	function myfav(){ //我关注的人的招贴
		$tie_category = $this->tie_category;

		$ties = array(); 

		$sqlaa = "SELECT `buddyid`  
							FROM `".TABLE_PREFIX."buddys` 
							WHERE `uid`={$_COOKIE['MEMBER_ID']}";
		$queryaa = $this->DatabaseHandler->Query($sqlaa);
		while( $rowAA = $queryaa->GetRow() ){
				//关注者的uid
				$uid = $rowAA['buddyid'];
			
				$sql = "select a.*,b.username,b.nickname,b.face from `".TABLE_PREFIX."tie` as a
							left join `".TABLE_PREFIX."members` as b on a.uid = b.uid
							where a.uid = $uid  order by a.addtime desc limit 5";
				$query = $this->DatabaseHandler->Query($sql);

				while($row = $query->GetRow()){
					$ties[] = $row;
				}
		}

		include(ROOT_PATH . './templates/app/tie_myfav.php');
	}
	
	
	function addsave(){	
		$tInfo = $this->Post;
		$sql = "insert into `".TABLE_PREFIX."tie` (`title`,`content`,`addtime`,`uid`,`pic`,`cate`) 
				values('{$tInfo['title']}','{$tInfo['content']}','".date('Y-m-d H:i:s')."',{$_COOKIE['MEMBER_ID']},'{$tInfo['pic']}',{$tInfo['tie_type']})";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('添加成功');location.href='index.php?mod=tie&code=my';</script>";
		echo $html;
	}	
	
	
	function del(){	
		$id = $this->Get['id'];
		$sql = "delete from `".TABLE_PREFIX."tie` where uid={$_COOKIE['MEMBER_ID']} and id=$id";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('删除成功');location.href='index.php?mod=tie&code=my';</script>";
		echo $html;
	}
	
}

?>
